package cn.comon.app.scan.service.analysis.impl;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Properties;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import cn.comon.app.scan.entity.AsReportinfo;
import cn.comon.app.scan.service.analysis.IAnalysisRequest;
import cn.comon.app.scan.util.UtilTools;

/**
 * 安全管家软件检测接口
 * @author xujf
 *
 */
@Service("aqgjRequestImpl")
@Scope("prototype")
@Lazy(value=true)
public class AQGJAnalysisRequestImpl implements IAnalysisRequest{
    private static Logger logger = Logger.getLogger(AQGJAnalysisRequestImpl.class.getName());
    public Object getSendData(Properties params, Object asReportinfo) {
        try {
            AsReportinfo asReportinfoTMP = (AsReportinfo)asReportinfo;
            // 安全管家接口数据
            JSONObject sendData = new JSONObject();
            
            // 构造接口访问地址
            String sendUrl = params.getProperty("aqgj.url");
            sendData.put("sendUrl", sendUrl);
           
            //获取需要检测的软件信息。
            ArrayList<String> formatList = new ArrayList<String>();
            //调用的接口名称-"a"
            formatList.add(params.getProperty("aqgj.a"));
            //渠道编号-"sc"
            formatList.add(params.getProperty("aqgj.sc"));
            //要检测的程序的package name-"pname"
            formatList.add(URLEncoder.encode(asReportinfoTMP.getPackageName(), "UTF-8"));
            //要检测的程序的vcode值(版本号)-"vcode"
            if (asReportinfoTMP.getVcode() == null || "".equals(asReportinfoTMP.getVcode())) {
                formatList.add("1");
            } else {
                formatList.add(asReportinfoTMP.getVcode());
            }
            //要检测的程序的签名信息序列号-"csn"
            if (asReportinfoTMP.getCsn() == null || "".equals(asReportinfoTMP.getCsn())) {
                //app的csn序列号无法取到
                formatList.add("1");
            } else {
                formatList.add(asReportinfoTMP.getCsn());
            }
            //要检测程序的apk的md5值(APK是AndroidPackage的缩写，即Android安装包(apk)。)-"apkm"
            formatList.add(URLEncoder.encode(asReportinfoTMP.getFileMd5(), "UTF-8"));
            //要检测程序的名称-"title"
            formatList.add(URLEncoder.encode(asReportinfoTMP.getPackageName(), "UTF-8"));
            //安装包的下载地址(选填)-"uri"
            formatList.add(URLEncoder.encode(asReportinfoTMP.getPackageUrl(), "UTF-8"));
            //md5(imei：是国际移动设备身份码的缩写)-"user"
            formatList.add(URLEncoder.encode("3711111111", "UTF-8"));
            //md5(apkm+sc+key)-"sign"
            String sign = asReportinfoTMP.getFileMd5() + params.getProperty("aqgj.sc") +
                          params.getProperty("aqgj.key");
            logger.info("【AQGJAnalysisRequestImpl.getSendData sign:" + sign + "】");
            formatList.add(UtilTools.getStringMd5(sign));
            
            //post内容构造
            String formatStyle = "a={0}&sc={1}&pname={2}&vcode={3}&csn={4}&apkm={5}&title={6}&uri={7}&user={8}&sign={9}";
            String post = UtilTools.formatStr(formatStyle, formatList);

            sendData.put("postData", post);
            logger.info("【AQGJAnalysisRequestImpl.getSendData post:" + post + "】");
            return sendData;
       } catch (Exception e) {
           e.printStackTrace();
           logger.error(e);
           return null;
        }
    }
}